<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" plain icon="el-icon-plus" @click="addInsuranceType" style="margin-bottom: 20px">添加</el-button>
    </el-row>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="informName" label="联系人姓名"> </el-table-column>
      <el-table-column align="center" prop="informPhone" label="联系人电话"> </el-table-column>
      <el-table-column align="center" prop="contactType" label="场景类别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.contactType === '1'" type="success">理赔语音通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="contactType" label="启用状态">
        <template slot-scope="scope">
          <el-switch :value="scope.row.enableStatus == '1'" @change="enableStatusChange(scope.row)" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editMsg(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="delMsg(scope.row.informPhoneId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="msgTitle" :visible.sync="dialogVisible" width="40%" v-loading="loading">
      <el-form :model="addForm" ref="addForm" :rules="rules" label-width="140px">
        <el-form-item label="联系人姓名" style="width: 100%" prop="informName">
          <el-input v-model="addForm.informName" placeholder="联系人姓名"></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" style="width: 100%" prop="informPhone">
          <el-input v-model="addForm.informPhone" placeholder="联系人电话"></el-input>
        </el-form-item>
        <el-form-item label="场景类别" style="width: 100%" prop="contactType" required>
          <el-select v-model="addForm.contactType">
            <el-option label="理赔语音通知" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cance">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      msgTitle: "添加",
      editId: "",
      addForm: {
        informName: "",
        informPhone: "",
        contactType: "1",
      },
      dialogVisible: false,
      // form: {
      //     user: '',
      //     region: ''
      // },
      tableData: [],
      tableHeight: 0,
      rules: {
        informName: [
          { required: true, message: "请输入联系人姓名", trigger: "blur" },
        ],
        informPhone: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    enableStatusChange(row) {
      const params = {
        informPhoneId: row.informPhoneId,
      };
      if (row.enableStatus == "0") {
        this.$post(`/config/informphone/enable`, params).then((res) => {
          if (res && res.code * 1 === 200) {
            this.$message({
              type: "success",
              message: "启用成功!",
            });
            this.getTableData();
          }
        });
      } else {
        this.$post(`/config/informphone/disable`, params).then((res) => {
          if (res && res.code * 1 === 200) {
            this.$message({
              type: "success",
              message: "禁用成功!",
            });
            this.getTableData();
          }
        });
      }
    },
    determine() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const url = this.editId ? "config/informphone" : "config/informphone";
          const func = this.editId ? "$put" : "$post";
          const params = this.editId
            ? { ...this.addForm, informPhoneId: this.editId }
            : this.addForm;
          this[func](url, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: this.editId ? "修改成功" : "新增成功",
                });
                this.$refs.addForm.resetFields();
                this.dialogVisible = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    delMsg(informPhoneId) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$delete(`/config/informphone/${informPhoneId}`).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getTableData();
            }
          });
        })
        .catch(() => {
          //
        });
    },
    cance() {
      this.$refs.addForm.resetFields();
      this.dialogVisible = false;
    },
    addInsuranceType() {
      this.$nextTick(() => {
        this.msgTitle = "添加";
        this.dialogVisible = true;
        this.addForm.informName = "";
        this.addForm.informPhone = "";
        this.addForm.contactType = "1";
        this.editId = "";
      });
    },
    editMsg(row) {
      this.$nextTick(() => {
        this.msgTitle = "修改";
        this.dialogVisible = true;
        this.addForm.informName = row.informName;
        this.addForm.informPhone = row.informPhone;
        this.addForm.contactType = row.contactType;
        this.editId = row.informPhoneId;
      });
    },
    getTableData() {
      this.$get("/config/informphone/list").then((res) => {
        this.tableData = res.data;
      });
    },
  },
  mounted() {
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>